package de.nasch.platz.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.nasch.platz.client.RestaurantService;
import de.nasch.platz.shared.Adresse;
import de.nasch.platz.shared.Restaurant;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class RestaurantServiceImpl extends RemoteServiceServlet implements
		RestaurantService {

	public List<String> getRestaurants() throws IllegalArgumentException {
		

        PersistenceManager pm1 = PMF.get().getPersistenceManager();		
		String query = "select from " + Restaurant.class.getName();
		@SuppressWarnings("unchecked")
		//List<Restaurant> restaurants = null;
		List<Restaurant> restaurants = (List<Restaurant>) pm1.newQuery(query).execute();
		List<String> restaurantNames = new ArrayList<String>();
		if(restaurants != null){
		for (Iterator iterator = restaurants.iterator(); iterator.hasNext();) {
			Restaurant restaurant = (Restaurant) iterator.next();
			restaurantNames.add(restaurant.getName());
		}
		}

		pm1.close();
		return restaurantNames;
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}
}
